package com.std;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DB {
	private String url="jdbc:oracle:thin:@127.0.0.1:1521:test";
	private String username="tiger";
	private String password="abc";
	private String classname="oracle.jdbc.driver.OracleDriver";
	private Connection con;
	private PreparedStatement pstmt;
	
	public DB(){
		try {
			Class.forName(classname);
			con=DriverManager.getConnection(url,username,password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void execute(String sql,Object [] params) {
		try {
			if(sql!=null&&!sql.equals("")){
				if(params==null){
					params=new Object[0];
				}
				if(con!=null){
					pstmt=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
					for(int i=0;i<params.length&&params[i]!=null;i++){
						//System.out.println("i:"+i+":"+params[i]);
						pstmt.setObject(i+1, params[i]);
					}
					pstmt.execute();
				}
			}			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
    public ResultSet getResultset(){
    	try {
    		return pstmt.getResultSet();
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
    }	
     public void close() {
		// TODO Auto-generated method stub
    	try {
        	 if(pstmt!=null){
        		 pstmt.close();
        	 }
        	 if(con!=null){
        		 con.close();
        	 }			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
    public static void main(String []args) throws SQLException{
    	DB db=new DB();
    	String sql="select * from user_t";
    	db.execute(sql, null);
    	ResultSet rs=db.getResultset();
    	while(rs.next()){
    		System.out.print("username"+rs.getString("username"));
    	}
		db.close();
    	System.out.println("DB.main()");
    }
}
